<?php

include( "./admin/include/func.php");

$db = new db();
if ( ! $db->connect()) exit( mysql_error());

$panels = array ();

$panel = array (
	'caption' => 'Hibajelentés',
	'title'   => 'Hibajelentési űrlap',
	'body'    => '',
	'height'  => '370'
);

array_push( $panels, $panel);
	
if (isset($_POST['elkuldes'])) {
	
	//print_r ($_POST);
	
    $p_nev        = trim(strip_tags($_POST['nev']));
    $p_email      = trim(strip_tags($_POST['email']));
    $p_rovleir    = trim(strip_tags($_POST['rovleir']));
    $p_leiras     = strip_tags($_POST['leiras']);
    $p_jelleg     = (int)$_POST['jelleg'];
    $p_gyakorisag = (int)$_POST['gyakorisag'];

    $success = true;
    $errors = array();

    if (($p_email) != '' && !preg_match('/^[\.\+_a-z0-9-]+@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}[mtgvu]?$/i', $p_email)) {
        $success = false;
        array_push($errors, "Hibás Email!");
    }
    
    if ($p_email == '') {
        $success = false;
        array_push($errors, "Az Email nincs megadva!");
    }
    
    if ($p_rovleir == '') {
        $success = false;
        array_push($errors, "A Rövid leírás nincs megadva!");
    } 
   
    if (($p_leiras) != '' && !preg_match('/^[\s\S]{0,500}$/u', $p_leiras)) {
        $success = false;
        array_push($errors, "A Leírás max 250 karakter lehet!");
    }

    if ( ! $success) {

		$body    = "<ul class='errors'>";
		foreach ($errors as $err) $body .= "<li>$err</li>";
		$body   .= "</ul>";
		
		$error_panel = array (
			'caption' => 'Hiba',
			'title'   => 'Kérjük javítsa ki az alábbi hibákat!',
			'body'    => $body,
			'height'  => count( $errors) * 25 + 10
		);
		
		array_unshift( $panels, $error_panel);
				
	} else {
		
		$p_userid  = User::get_id();
		$p_ipaddr  = $_SERVER['REMOTE_ADDR'];
		
		$db->sql = "INSERT INTO bug (id, createdon, nev, mail, rovleir, leiras, jelleg, gyakorisag, userid, ipaddr) 
		            VALUES ('', NOW(),'$p_nev', '$p_email', '$p_rovleir', '$p_leiras', '$p_jelleg', '$p_gyakorisag', '$p_userid', '$p_ipaddr')";
		$db->query();
		 
		$message = "feladó név: $p_nev
feladó email: $p_email
feladó userid: $p_userid
feladó ip: $p_ipaddr
rövid leírás: $p_rovleir
részletes leírás: $p_leiras
jelleg: $p_jelleg
elõfordulási gyak.: $p_gyakorisag"; 

		mail( $admin_mail, "Hibajelentés", $message, "From: ".$noreply);  
		
		//print $message;
		
		$panels = array ();
		
		$panel = array (
			'caption' => 'Információ',
			'title'   => 'A hibajelentést rögzítettük!',
			'body'    => "<p class='success'>Köszönjük, hogy elküldte a hibaleírást!<br />Megpróbáljuk a hibát minnél elöbb kijavítani és szükség esetén email-ben értesítjük.</p>",
			'height'  => '55'
		);

		array_push( $panels, $panel);

	}
}
		    
?>

<?php include ("header.php"); ?>

<?php include ("top.php"); ?>

<?php include ("left.php"); ?>
							
<div id="middle">
	
<?php foreach( $panels as $panel) { ?>
	
	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption"><?php print $panel['caption']; ?></p>
				<p class="hptitle"><?php print $panel['title']; ?></p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="<?php print $panel['height']; ?>px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:<?php print $panel['height']; ?>px">
				<?php if ($panel['body'] != '') print $panel['body']; else { ?>
					<form name="form_kereses" id="form_kereses" method="post" enctype='application/x-www-form-urlencoded' accept-charset="utf-8">

						<label for='nev'>Név</label>
						<input name='nev' type='text' value="<?php print isset($p_nev)?$p_nev:''; ?>" />
						<br />

						<label for='email' class="required">Email</label>
						<input name='email' type='text' value="<?php if (isset( $_SESSION['email'])) print $_SESSION['email']; else if (isset( $p_email)) print $p_email; ?>" />
						<br />

						<label for='rovleir' class="required">Rövid leírás</label>
						<input name='rovleir' type='text' value="<?php print isset($p_rovleir)?$p_rovleir:''; ?>" />
						<br />

						<label for='leiras'>Részletes leírás</label>
						<textarea cols="39" rows="7" id='leiras' name='leiras' cols='39' rows='7' onKeyDown="textCounter('leiras',250,'text-counter')" onKeyUp="textCounter('leiras',250,'text-counter')"><?php print isset($p_leiras)?$p_leiras:''; ?></textarea>
						<div class="note"><span>A leírás még max </span><input id="text-counter" type="text" value="250" /><span> karakter lehet.</span></div>
						<script>textCounter('leiras',250,'text-counter')</script>
						<br />				
										
						<label for='jelleg'>Jelleg</label>
						<select name='jelleg'> 
							<option value="0" <?php print isset($p_jelleg) && $p_jelleg==0?"selected='selected'":''; ?>>Nem tudom</option> 
							<option value="1" <?php print isset($p_jelleg) && $p_jelleg==1?"selected='selected'":''; ?>>Funkcionális</option>
							<option value="2" <?php print isset($p_jelleg) && $p_jelleg==2?"selected='selected'":''; ?>>Kozmetikai</option>
							<option value="3" <?php print isset($p_jelleg) && $p_jelleg==3?"selected='selected'":''; ?>>Nem jellemezhetõ</option>                 
						</select>
						<br />
										
						<label for='gyakorisag'>Gyakoriság</label>
						<select name='gyakorisag'>  
							<option value="0" <?php print isset($p_gyakorisag) && $p_gyakorisag==0?"selected='selected'":''; ?>>Nem tudom</option>  
							<option value="1" <?php print isset($p_gyakorisag) && $p_gyakorisag==1?"selected='selected'":''; ?>>Állandóan</option>
							<option value="2" <?php print isset($p_gyakorisag) && $p_gyakorisag==2?"selected='selected'":''; ?>>Gyakran</option>
							<option value="3" <?php print isset($p_gyakorisag) && $p_gyakorisag==3?"selected='selected'":''; ?>>Néha</option>
							<option value="4" <?php print isset($p_gyakorisag) && $p_gyakorisag==4?"selected='selected'":''; ?>>Ritkán</option>
							<option value="5" <?php print isset($p_gyakorisag) && $p_gyakorisag==5?"selected='selected'":''; ?>>Nem próbáltam</option>             
						</select>
						<br />

						<br />
											
						<label for="elkuldes">&nbsp;</label>
						<input name="elkuldes" type="submit" value="Elküldés"/>				

					</form>  
				<?php } ?>           			
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="<?php print $panel['height']; ?>px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>

	<br />
	<br />

<?php } ?>

</div>

<?php include ("right.php"); ?>

<?php include ("footer.php"); ?>
